<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('AlohacartExtensionModel')) return;
/**
 * Aloha Cart
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage alohacart
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/AlohaCart
 */

//require_once(_DIR_PLUGIN_PDK.'classes/models/PdkAuteurModel.php');

/** 
 *  This class is responsible for working with alohacart extensions
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
include_spip('functions/pdk_common');
pdk_load_class('pdk_model', 'classes/');
class AlohacartExtensionModel extends pdk_Model {    
    public function __construct() {
        parent::__construct();        
        $this->config_model();        
    }
    
    function config_model() {
        // Define main table:
        $this->main_table = 'spip_alohacart_extensions';
        
        // Define primary key:
        $this->primary_key = 'id_extension';
        
        // Define main table fields:
        $this->main_fields['id_extension'] = 'id_extension';
        $this->main_fields['titre'] = 'titre';
        $this->main_fields['type'] = 'type';                                                       
        $this->main_fields['code'] = 'code';
        $this->main_fields['active'] = 'active';
          
         // Define validation rules:         
         $this->validation->set_rules( 
            array(
               array(
                     'field'   => 'id_extension',
                     'label'   => 'id_extension',
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'type',
                     'label'   => 'Type',
                     'rules'   => ''
                  ),
               array(
                     'field'   => 'titre',
                     'label'   => _T('alohacart:titre'),
                     'rules'   => 'required'
                  ),   
               array(
                     'field'   => 'code',
                     'label'   => 'Code',
                     'rules'   => ''
                  )   
            )
         );                             
    }
    
    // Define test data
    public $test_data = array(
        array('id_extension'=>1, 'titre'=>'Flat shipping','type'=>'shipping', 'code'=>'flat', 'active'=>1),
        array('id_extension'=>2, 'titre'=>'Weight shipping','type'=>'shipping', 'code'=>'Weight', 'active'=>'0'),
        array('id_extension'=>3, 'titre'=>'Bank transfer', 'type'=>'payment', 'code'=>'BankTransfer', 'active'=>'1'),
        array('id_extension'=>4, 'titre'=>'Webmoney','type'=>'payment', 'code'=>'Webmoney', 'active'=>'0'),
    );
    
    public function form_data_from_array($data, $only_hard_join_data=true){
        $data = parent::form_data_from_array($data, $only_hard_join_data=true);
        
        if ($data['active']=='on') 
            $data['active'] = 1; 
        else if (!$data['active']) 
            $data['active'] = 0;
        
        return $data;
    }
    
    /**
     *  Finds extension class, includes it's file and returns an instance of this class according to given data
     */
    public function GetExtensionClass($where) {
        $extension = $this->select($where);
        $extension = $extension[0];
        $extension_for_class = $extension;
        $extension_for_class['type'][0] = strtoupper($extension['type'][0]);
        $extension_for_class['code'][0] = strtoupper($extension['code'][0]);
        return pdk_load_class('Alohacart'.$extension_for_class['type'].$extension_for_class['code'], 'classes/'.$extension['type']);
    }
    
    /**
     *  Finds extension class, includes it's file and returns an instance of this class according to $id_extension
     */
    public function GetExtensionClassByID($id_extension) {
        return $this->GetExtensionClass("id_extension='".$id_extension."'");
    } 
    
    /**
     *  Finds extension class, includes it's file and returns an instance of this class according to $code
     */
    public function GetExtensionClassByCode($code) {
        return $this->GetExtensionClass("code='".$code."'");
    }
}
?>